﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;

namespace T3Task5
{
    class DBHelper
    {
        //静态字段
        public static string connStr = "server=.;database=Admin;Integrated Security=true ";
        //静态方法一查询
        public static SqlDataReader Select(string sql)
        {
            //1、创建连接对象
            SqlConnection conn = new SqlConnection(connStr);
            //2、打开连接
            conn.Open();
            //3、创建对象命令
            SqlCommand cmd = new SqlCommand(sql, conn);
            //4、ExecuteReader执行
            return cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
        }

        //执行查询操作，获取DataSet
        public static DataSet GetDataSet(string sqlSet)
        {
            //1、创建连接对象
            SqlConnection conn = new SqlConnection(connStr);
            //2、打开连接
            conn.Open();
            //3、创建对象命令
            DataSet ds = new DataSet();
            SqlDataAdapter dap = new SqlDataAdapter(sqlSet,conn);
            dap.Fill(ds);
            conn.Close();
            return ds;
        }

        //执行查询操作，获取DataTable
        /* public static DataTable GetDataTable(string sqlStr)
          {

              return GetDataTable(sqlStr).Tables[0];
          }*/


        //静态方法二增删改
        public static bool GetNonQuery(string sql)
        {
            bool result = false;
            //1、创建连接对象
            SqlConnection conn = new SqlConnection(connStr);
            //2、打开连接
            conn.Open();
            //3、创建对象命令
            SqlCommand cmd = new SqlCommand(sql, conn);
            //4、ExecuteNonQuery执行(增删改)
            int number = cmd.ExecuteNonQuery();
            if (number > 0)
            {
                result = true;
            }
            conn.Close();
            return result;

        }

        //执行集合函数操作
        public static object ExecuteScalar(string sqlStr)
        {
            //1、创建连接对象
            SqlConnection conn = new SqlConnection(connStr);
            //2、打开连接
            conn.Open();
            //3、创建对象命令
            SqlCommand cmd = new SqlCommand(sqlStr,conn);
            object result=cmd.ExecuteScalar();
            conn.Close();
            return result;

        }

    }
}

 
